{include file="public/header" /}
<span class="layui-breadcrumb">
  <a href="{:url('Blog/index')}?nav=blog-bloglist">博客</a>
  <a><cite>{if !empty($data_list['id']) }编辑{else/}添加{/if}博客</cite></a>
</span>
<style>
    .layui-form-label{width:200px;}
    .layui-form-item .layui-input-block{margin-left:230px;}
    .tox-tinymce-aux {
        z-index: 5000 !important;
    }
    .tox-fullscreen .layui-side-menu{
        z-index: 0 !important;
    }
</style>
<!--<script src="https://cdn.tiny.cloud/1/fuclxmtuqcgjlv66r58qtuu0y433gpi4kf0kbsq34rqcqvlv/tinymce/6/tinymce.min.js" referrerpolicy="origin"></script>-->
<script type="text/javascript" src="/static/tinymce/tinymce.min.js" referrerpolicy="origin"></script>
<div class="layui-fluid">
    <div class="layui-card">
      <div class="layui-card-body">
        <div style="color:#FF5722">{if !empty($data_list['id']) }编辑{else/}添加{/if}博客,返回页面后请刷新</div>
        <div class="layui-form">
         <form class="layui-form" action="" style='padding:10px;'>
          <div class="layui-form-item">
            <label class="layui-form-label">标题</label>
            <div class="layui-input-block">
                <input type="text" name="title" value="{if !empty($data_list['title']) }{$data_list['title']}{/if}" autocomplete="off" placeholder="请输入标题" class="layui-input">
            </div>
          </div>

          <div class="layui-form-item">
            <label class="layui-form-label">作者</label>
            <div class="layui-input-block">
                <!--<input type="text" name="author" value="{if !empty($data_list['author']) }{$data_list['author']}{/if}" autocomplete="off" placeholder="请输入作者" class="layui-input">-->
                <select name="author" id='author' lay-filter="aihao">
                    <option value=""></option>
                    {foreach $author as $key=>$vo }
                    {if !empty($data_list['author']) }
                    {if $data_list['author']==$vo['nickname']}
                    <option value="{$vo['nickname']}" selected="">{$vo['nickname']}</option>
                    {else /}
                    <option value="{$vo['nickname']}">{$vo['nickname']}</option>
                    {/if}
                    {else /}
                    <option value="{$vo['nickname']}">{$vo['nickname']}</option>
                    {/if}
                    {/foreach}

                </select>
            </div>
          </div>
          
           <div class="layui-form-item">
            <label class="layui-form-label">分类</label>
            <div class="layui-input-block">
              <select name="class_zhi" id='class_zhi' lay-filter="aihao">
                <option value=""></option>
                {foreach $blog as $key=>$vo }
                {if !empty($data_list['blogclass']) }
                {if $data_list['blogclass']['class_id']==$vo['class_id']}
                <option value="{$vo['class_id']}" selected="">{$vo['title']}</option>
                {else /}
                <option value="{$vo['class_id']}">{$vo['title']}</option>
                {/if}
                {else /}
                <option value="{$vo['class_id']}">{$vo['title']}</option>
                {/if}
                {/foreach}
                
              </select>
            </div>
          </div>
         {if !isset($data_list) }
         <div class="layui-form-item">
             <label class="layui-form-label">是否用描点</label>
             <div class="layui-input-block">
                 <input type="radio" name="miaodian" value="1" title="是" >
                 <input type="radio" name="miaodian" value="0" title="否">
             </div>
         </div>
         {else/}
          <div class="layui-form-item">
             <label class="layui-form-label">是否用描点</label>
             <div class="layui-input-block">
                 <input type="radio" name="miaodian" value="1" <?php if($data_list['miaodian']==1){echo "checked";}?> title="是">
                 <input type="radio" name="miaodian" value="0" <?php if($data_list['miaodian']==0){echo "checked";}?> title="否">
             </div>
         </div>
         {/if}
          <div class="layui-form-item">
              <label class="layui-form-label">内容</label>
              <div class="layui-input-block">
                <!--<textarea id="content" name="content" style="width:100%;height:300px;">{if !empty($data_list['content']) }{$data_list['content']}{/if}</textarea>-->
                  <textarea id="content" name="content" style="width:100%;height:300px;">{$data_list['content']|raw|default=''}</textarea>
              </div>
          </div>

          <div class="layui-form-item">
            <label class="layui-form-label">SEO标题</label>
            <div class="layui-input-block">
                <input type="text" name="seo_title" value="{if !empty($data_list['seo_title']) }{$data_list['seo_title']}{/if}" autocomplete="off" placeholder="请输入SEO标题" class="layui-input">
            </div>
          </div>

          <div class="layui-form-item">
            <label class="layui-form-label">SEO关键字</label>
            <div class="layui-input-block">
                <input type="text" name="seo_key" value="{if !empty($data_list['seo_key']) }{$data_list['seo_key']}{/if}" autocomplete="off" placeholder="请输入SEO关键字" class="layui-input">
            </div>
          </div>

          <div class="layui-form-item">
            <label class="layui-form-label">SEO描述</label>
            <div class="layui-input-block">
                <input type="text" name="seo_desc" value="{if !empty($data_list['seo_desc']) }{$data_list['seo_desc']}{/if}" autocomplete="off" placeholder="请输入SEO描述" class="layui-input">
            </div>
          </div>

          <div class="layui-form-item">
            <label class="layui-form-label">url</label>
            <div class="layui-input-block">
                <input type="text" name="url" value="{if !empty($data_list['url']) }{$data_list['url']}{/if}" autocomplete="off" placeholder="请输入url" class="layui-input">
            </div>
          </div>

          <div class="layui-form-item">
            <label class="layui-form-label">列表图</label>
            <div class="layui-input-block">
              <div class="layui-upload">
              <button type="button" class="layui-btn layui-btn-xs" id="upload_img">选择图片</button>
              <span style='color:#FF5722;font-size:12px'>视频及ie8不支持预览</span>
              <div class="layui-upload-list">
                <img class="layui-upload-img" style='width:200px' id="show_img" src="{if !empty($data_list['img']) }{$data_list['img']}{/if}">
                <p id="demoText"></p>
              </div>
            </div> 
            </div>
            <input type="text" name="newname" style="display:none" value="{if !empty($data_list['img']) }{$data_list['img']}{/if}">
          </div>
          <div class="layui-form-item">
            <label class="layui-form-label"></label>
            <div class="layui-input-block">
              <button type="button" class="layui-btn layui-btn-sm" onclick="insert_edit({if !empty($data_list['id']) }{$data_list['id']}{else/}'tj'{/if})">确 定</button>
            </div>
          </div>
          
        </form>

        </div>
      </div>  
    </div>
</div>
{include file="/public/footer" /}

<script>
layui.use('upload', function(){
  var $ = layui.jquery
  ,upload = layui.upload;
  
  //普通图片上传
  var uploadInst = upload.render({
    elem: '#upload_img'
    ,accept: 'file'
    ,url: "{:url('index/Oos/upload_blog_img')}" //改成您自己的上传接口
    ,before: function(obj){
      //预读本地文件示例，不支持ie8
      obj.preview(function(index, file, result){
        $('#show_img').attr('src', result); //图片链接（base64）
      });
    }
    ,done: function(res){
      //如果上传失败
      if(res.status == '0'){
        return layer.msg(res.msg);
      }else{ //上传成功
        $("input[name='newname']").val(res.newname);
        return layer.msg(res.msg);
      }
    }
    ,error: function(){
      //演示失败状态，并实现重传
      var demoText = $('#demoText');
      demoText.html('<span style="color: #FF5722;">上传失败</span> <a class="layui-btn layui-btn-xs demo-reload">重试</a>');
      demoText.find('.demo-reload').on('click', function(){
        uploadInst.upload();
      });
    }
  });
});  
</script>
<script charset="utf-8" src="/static/kindeditor/kindeditor-all.js?t={:time()}"></script>
<script charset="utf-8" src="/static/kindeditor/lang/zh-CN.js"></script>
<script>
    tinymce.init({
        selector: '#content',
        plugins: 'ai tinycomments mentions anchor autolink charmap codesample emoticons image link lists media searchreplace table visualblocks wordcount checklist mediaembed casechange export formatpainter pageembed permanentpen footnotes advtemplate advtable advcode editimage tableofcontents mergetags powerpaste tinymcespellchecker autocorrect a11ychecker typography inlinecss code image axupimgs preview fullscreen link ',
        toolbar: 'undo redo | blocks fontfamily fontsize | bold italic underline strikethrough | link image media table mergetags | align lineheight | tinycomments | checklist numlist bullist indent outdent | emoticons charmap | removeformat | code axupimgs | fullscreen | link',
        tinycomments_mode: 'embedded',
        tinycomments_author: 'Author name',
        mergetags_list: [
            { value: 'First.Name', title: 'First Name' },
            { value: 'Email', title: 'Email' },
        ],
        language_url: '/static/tinymce/langs/zh-Hans.js', // 中文包的存放路径
        language: 'zh-Hans',
        //language: 'zh_CN',
        allow_html_in_named_anchor: true,
        //images_upload_url: '/index/Oos/tinymce_upload',
        images_upload_base_path: '/index',
        image_title: true,
        file_picker_callback: function (callback, value, meta) {
            //文件分类
            var filetype='.pdf, .txt, .zip, .rar, .7z, .doc, .docx, .xls, .xlsx, .ppt, .pptx, .mp3, .mp4';
            //后端接收上传文件的地址
            var upurl='/index/Oos/tinymce_upload';
            //为不同插件指定文件类型及后端地址
            switch(meta.filetype){
                case 'image':
                    filetype='.jpg, .jpeg, .png, .gif';
                    upurl='/index/Oos/tinymce_upload';
                    break;
                case 'media':
                    filetype='.mp3, .mp4';
                    upurl='/index/Oos/tinymce_upload';
                    break;
                case 'file':
                default:
            }
            //模拟出一个input用于添加本地文件
            var input = document.createElement('input');
            input.setAttribute('type', 'file');
            input.setAttribute('accept', filetype);
            input.click();
            input.onchange = function() {
                var file = this.files[0];

                var xhr, formData;
                console.log(file.name);
                xhr = new XMLHttpRequest();
                xhr.withCredentials = false;
                xhr.open('POST', upurl);
                xhr.onload = function() {
                    var json;
                    if (xhr.status != 200) {
                        failure('HTTP Error: ' + xhr.status);
                        return;
                    }
                    json = JSON.parse(xhr.responseText);
                    if (!json || typeof json.location != 'string') {
                        failure('Invalid JSON: ' + xhr.responseText);
                        return;
                    }
                    callback(json.location);
                };
                formData = new FormData();
                formData.append('file', file, file.name );
                xhr.send(formData);

            };
        },
        setup : function(ed) {
            ed.on("change", function(e){
                $('#content').html(tinymce.activeEditor.getContent());
            });
        },

        ai_request: (request, respondWith) => respondWith.string(() => Promise.reject("See docs to implement AI Assistant")),
    });
</script>
<script>
  KindEditor.ready(function(K) {
      /*
    window.editor=K.create('#content',{'items':['source', '|', 'undo', 'redo', '|','selectall', 'preview', 'print', 'code', 'cut', 'copy', 'paste','plainpaste', 'wordpaste', '|', 'justifyleft', 'justifycenter', 'justifyright','justifyfull', 'insertorderedlist', 'insertunorderedlist', 'indent', 'outdent', 'subscript','superscript', 'clearhtml', 'quickformat', '|','formatblock', 'fontname', 'fontsize', '|', 'forecolor', 'hilitecolor', 'bold','italic', 'underline', 'strikethrough', 'lineheight', 'removeformat', '|', 'image', 'media', 'insertfile', 'table', 'hr', 'emoticons', 'baidumap', 'pagebreak','anchor', 'link', 'unlink','|', 'fullscreen'],'uploadJson':"{:url('index/Oos/upload_blog')}",afterCreate : function() { this.sync(); },afterBlur: function(){this.sync();}

    });
    */


  })

  function insert_edit(id){
      layer.load();
      var title=$.trim($("input[name='title']").val());
     // var author=$.trim($("input[name='author']").val());
      var author=$.trim($("#author").val());
      var newname=$.trim($("input[name='newname']").val());
      var seo_title=$.trim($("input[name='seo_title']").val());
      var seo_key=$.trim($("input[name='seo_key']").val());
      var seo_desc=$.trim($("input[name='seo_desc']").val());
      var url1=$.trim($("input[name='url']").val());
      var content=$.trim($("textarea[name='content']").val());
      var class_zhi=$.trim($("#class_zhi").val());
      var miaodian=$("input[name='miaodian']:checked").val();
      if(title==''){
          layer.closeAll('loading');
          layer.msg('请填写标题');
          return false;
      }
      /*
      if(author==''){
          layer.closeAll('loading');
          layer.msg('请填写作者');
          return false;
      }
      */
      if(author==''){
          layer.closeAll('loading');
          layer.msg('请选择作者');
          return false;
      }
      if(class_zhi==''){
          layer.closeAll('loading');
          layer.msg('请选择分类');
          return false;
      }
      if(content==''){
          layer.closeAll('loading');
          layer.msg('请填写内容');
          return false;
      }
      if(seo_title==''){
          layer.closeAll('loading');
          layer.msg('请填写SEO标题');
          return false;
      }

      if(url1==''){
          layer.closeAll('loading');
          layer.msg('请填写url');
          return false;
      }

      if(newname==''){
          layer.closeAll('loading');
          layer.msg('请上传列表图');
          return false;
      }

      if(id=='tj'){
        var url="{:url('Blog/do_change')}";
      }else{
        var url="{:url('Blog/do_change')}"+"?id="+id;
      }
      $.post(url,{title:title,author:author,newname:newname,content:content,seo_title:seo_title,seo_key:seo_key,seo_desc:seo_desc,url1:url1,class_zhi:class_zhi,miaodian:miaodian},function(res){
        if(res.status=='0'){
            layer.closeAll('loading');
            layer.msg(res.msg);
        }
        if(res.status=='1'){
            layer.msg(
            res.msg,{time: 1500 },
            function(){
              layer.closeAll();
              location.href=location.href;
            });
        }
    }) 
  }
</script>


